package com.ctg.itrdc.k8sconsole.control;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.imageconsole.common.bean.CommonResult;
import com.ctg.itrdc.imageconsole.common.bean.ReturnCodeDefine;
import com.ctg.itrdc.imageconsole.data.dto.ClusterAssignDTO;
import com.ctg.itrdc.imageconsole.data.dto.image.MicroUserDTO;
import com.ctg.itrdc.imageconsole.data.entity.ClusterConfig;
import com.ctg.itrdc.imageconsole.service.IClusterConfigService;

/**
 * 
 * @description: 集群相关
 * @version: v1.0.0
 * @author: Piao.Wu
 * 
 *          Modification History: Date Author Version Description
 *          -----------------------------------------------------------------
 *          2016年9月1日 下午7:56:58 Piao.Wu v1.0.0 create
 *
 */
@Controller
@RequestMapping("/cluster")
public class ClusterController {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	private IClusterConfigService clusterService;

	/**
	 * 
	 * @Description: 集群信息查询
	 * @author Piao.Wu 2016年9月8日 上午10:59:20
	 * @return CommonResult
	 *
	 */
	@RequestMapping(value = "/queryClusterList", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryClusterList() {
		try {
			List<ClusterConfig> clusterConfigs = clusterService.queryClusterList();
			if (null != clusterConfigs) {
				return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, clusterConfigs);
			}
			clusterConfigs = new ArrayList<ClusterConfig>();
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, clusterConfigs);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}

	@RequestMapping(value = "/addCluster", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult addCluster(@RequestBody ClusterConfig clusterConfig) {
		try {
			String message = clusterService.addCluster(clusterConfig);
			// 如果返回的null，则说明新增时的一些检测通过
			if (message == null) {
				return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000);
			} else {
				// 不为null，就将错误消息message返回给前端
				return new CommonResult(ReturnCodeDefine.Code.C1200, message);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}

	@RequestMapping(value = "/queryCluster", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryCluster(@RequestParam("id") Long id) {
		try {
			ClusterConfig clusterConfig = clusterService.queryCluster(id);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, clusterConfig);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}


	/**
	 * 
	 * @Description: 集群删除
	 * @author Piao.Wu 2016年9月8日 上午10:56:08
	 * @param ClusterConfig
	 * @return CommonResult
	 *
	 */
	@RequestMapping(value = "/deleteCluster", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult deleteCluster(@RequestBody ClusterConfig ClusterConfig) {
		try {
			clusterService.deleteByPrimaryKey(ClusterConfig);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}
	
	
	@RequestMapping(value = "/assign", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult clusterAssign(@RequestBody ClusterAssignDTO clusterShareDTO) {
		try {
			clusterService.clusterAssign(clusterShareDTO);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000);
		} catch (Exception e) {
			logger.error("/cluster/assign 接口出现异常！",e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage(), null);
		}
	}
	
	
	@RequestMapping(value = "/queryAssignUsers", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryAssignUsers(@RequestParam("clusterConfigId") Long clusterConfigId) {
		try {
			List<MicroUserDTO> shareDTOs = clusterService.queryAssignUsers(clusterConfigId);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, shareDTOs);
		} catch (Exception e) {
			logger.error("/cluster/queryAssignUsers 接口出现异常！",e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage(), null);
		}
	}

}
